package com.Atao.controller;

import com.Atao.pojo.Result;
import com.Atao.service.FileDelete;
import com.Atao.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.UUID;

@RestController
@RequestMapping
public class FileController {

    @Autowired
    private FileDelete fileDelete;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String filename =UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
        return Result.success(url);
    }

    @DeleteMapping("/delete")
    public Result deleteFile(@RequestParam String url) {
            String fileName = url.substring(url.lastIndexOf("/") + 1);
            fileDelete.deleteFile(fileName);
            return Result.success();
    }
}